package com.crazy.crazyuserservice.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.exception.BusinessException;
import com.crazy.crazycommon.properties.JwtProperties;
import com.crazy.crazycommon.utils.JWTUtils;
import com.crazy.crazymodel.model.entity.Admin;
import com.crazy.crazymodel.model.vo.LoginAdminVO;
import com.crazy.crazyuserservice.service.AdminService;
import com.crazy.crazyuserservice.mapper.AdminMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 操作员服务实现
 */

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    public static final String SALT = "crazy!";

    @Resource
    private JwtProperties jwtProperties;

    @Override
    public LoginAdminVO adminLogin(String adminAccount, String adminPassword, HttpServletRequest request) {
        // 1. 校验
        if (StringUtils.isAnyBlank(adminAccount, adminPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (adminAccount.length() < 4) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号错误");
        }
        if (adminPassword.length() < 8) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
        }
        // 2. 加密
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + adminPassword).getBytes());
        // 查询用户是否存在
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminAccount", adminAccount);
        queryWrapper.eq("adminPassword", encryptPassword);
        Admin admin = this.getBaseMapper().selectOne(queryWrapper);
        // 用户不存在
        if (admin == null) {
            log.info("user login failed, adminAccount cannot match adminPassword");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
        }

        //3.生成token
        Map<String, Object> claims = new HashMap<>();

        claims.put("adminID", admin.getId());
        String jwt = JWTUtils.createJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);

        LoginAdminVO loginAdminrVO = this.getLoginAdminVO(admin);
        loginAdminrVO.setToken(jwt);
        log.info("操作员登录:{}",loginAdminrVO);
        return loginAdminrVO;
    }

    @Override
    public LoginAdminVO getLoginAdminVO(Admin admin) {
        if (admin == null) {
            return null;
        }
        LoginAdminVO loginAdminVO = new LoginAdminVO();
        BeanUtils.copyProperties(admin, loginAdminVO);
        return loginAdminVO;
    }
}
